package Solution201_220;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class Solution205 {
    public boolean isIsomorphic(String s, String t) {
        Map<Character, Integer> charMap = new HashMap<>();
        if (s == null || s.length() == 1) {
            return true;
        }
        for (int i = 0; i < s.length(); i++) {
            char sC = s.charAt(i);
            char tC = t.charAt(i);
            if (charMap.containsKey(sC)) {
                if (charMap.get(sC) != (tC - sC)) {
                    return false;
                }
            } else {
                charMap.put(sC, tC - sC);
            }
            if (i > 0) {
                char pSC = s.charAt(i - 1);
                char pTC = t.charAt(i - 1);
                if ((pSC - sC == 0 && pTC - tC != 0) || pSC - sC != 0 && pTC - tC == 0) {
                    return false;
                }
            }
        }
        return true;
    }

    @Test
    public void test() {
        String s = "egg";
        String t = "add";
        System.out.println(isIsomorphic(s, t));
    }
}
